const $schedule = require('node-schedule')

/** 回车换为空格 */
function changeToArray (str) {
  return str.replace(/[\r\n]/g, ' ').split(' ').filter(v => v !== '')
}
/** 定时任务*/
function schedule (corn) {
  return new Promise(resolve => {
    $schedule.scheduleJob(corn, resolve)
  })
}
/** sleep任务 */
function sleep (t) {
  return new Promise(resolve => setTimeout(resolve, t))
}


/**
 * 消息格式
 * @type 0 today | 1 tomorrow
 * @param {
  *  date: string,
  *  cond_txt: string,
  *  wind_dir:string,
  *  ...
  * }
  */
const getDay = (type) => {
  let today = new Date()
  let tomorrow = new Date(new Date().setTime(new Date().getTime() + 24 * 60 * 60 * 1000))
  let afterTomorrow = new Date(new Date().setTime(new Date().getTime() + 24 * 60 * 60 * 1000 * 2))
  // let date = type === 0 ? today : tomorrow
  let day_list = [{ text: '今天', date: today }, { text: '明天', date: tomorrow }, { text: '后天', date: afterTomorrow }]
  /** 得出具体日期 */
  let _obj = day_list[type]
  return {
    text: _obj.text,
    date: `${_obj.date.getFullYear()}年${_obj.date.getMonth() + 1}月${_obj.date.getDate()}日`
  }
}

const tipText = (type, params) => `降雨提醒！上海市闵行区 ${getDay(type).date}(${getDay(type).text})\n
 --------------------
 \n${getDay(type).text}白天：${params.cond_txt_d}，${getDay(type).text}夜里：${params.cond_txt_n}，${params.wind_dir}，${params.wind_sc}级，最高温度：${params.tmp_max}，最低温度：${params.tmp_max}\n
 --------------------
 \n${new Date().toTimeString()}自动发送`

module.exports = {
  changeToArray,
  schedule,
  tipText,
  sleep
}

// https://api.qrserver.com/v1/create-qr-code/?data=$$$ 自动转二维码链接
